package org.devocean.groupbuying.web.backend;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.devocean.groupbuying.domain.Tags;
import org.devocean.groupbuying.service.TagsService;
import org.devocean.groupbuying.utils.RenderMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value="/gb-cpanel/tags")
public class TagsController {
	
	@Autowired
	private TagsService tagsService; 

	@RequestMapping(value="/",method=RequestMethod.GET)
	public Map<String, ? extends Object> getAllMerchants(
			@RequestParam(value = "query",defaultValue="") String query,
			@RequestParam(value = "start", required = false,defaultValue="0") Integer start,
			@RequestParam(value = "limit", required = false,defaultValue="0") Integer size)
	{
		
		try {
		List<Tags> tags = tagsService.getTagsByCriteria(query,start,size);
		
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for (Tags tag : tags) {
			Map<String, Object> data = new ConcurrentHashMap<String, Object>();
			data.put("tagsID", tag.getTagsID());
			data.put("title", tag.getTitle());
			result.add(data);
		}
		
		long total = tagsService.countTagsByCriteria(query);
		return RenderMapUtils.getMap(result, total);
		}catch (Exception e) {
			return RenderMapUtils
					.getModelMapError("Error retrieving Merchants from database.");
		}
	}
}
